﻿using System;
using System.Collections.Generic;
using System.Text;
using UtilZ.Dotnet.Ex.Model;

namespace UtilZ.Dotnet.Ex.Communication.Net.Web
{
    /// <summary>
    /// web序列化器基类
    /// </summary>
    public abstract class WebSerializerAbs : IWebSerializer
    {

        /// <summary>
        /// 构造函数
        /// </summary>
        public WebSerializerAbs()
        {

        }





        /// <summary>
        /// 将对象序列化为xml
        /// </summary>
        /// <param name="obj">要序列化的对象</param>
        /// <returns>xml</returns>
        public string XmlSerialize(object obj)
        {
            return this.PrimitiveXmlSerialize(obj);
        }

        /// <summary>
        /// 将对象序列化为xml
        /// </summary>
        /// <param name="obj">要序列化的对象</param>
        /// <returns>xml</returns>
        protected abstract string PrimitiveXmlSerialize(object obj);






        /// <summary>
        /// 将xml反序列化为指定类型的对象
        /// </summary>
        /// <param name="xml">xml</param>
        /// <param name="targetType">目标类型</param>
        /// <returns>结果对象</returns>
        public object XmlDeserialize(string xml, Type targetType)
        {
            return this.PrimitiveXmlDeserialize(xml, targetType);
        }

        /// <summary>
        /// 将xml反序列化为指定类型的对象
        /// </summary>
        /// <param name="xml">xml</param>
        /// <param name="targetType">目标类型</param>
        /// <returns>结果对象</returns>
        protected abstract object PrimitiveXmlDeserialize(string xml, Type targetType);







        /// <summary>
        /// 将对象序列化为json
        /// </summary>
        /// <param name="obj">要序列化的对象</param>
        /// <returns>json</returns>
        public string JsonSerialize(object obj)
        {
            return this.PrimitiveJsonSerialize(obj);
        }

        /// <summary>
        /// 将对象序列化为json
        /// </summary>
        /// <param name="obj">要序列化的对象</param>
        /// <returns>json</returns>
        protected abstract string PrimitiveJsonSerialize(object obj);






        /// <summary>
        /// 将json反序列化为指定类型的对象
        /// </summary>
        /// <param name="json">json</param>
        /// <param name="targetType">目标类型</param>
        /// <returns>结果对象</returns>
        public object JsonDeserialize(string json, Type targetType)
        {
            return this.PrimitiveJsonDeserialize(json, targetType);
        }

        /// <summary>
        /// 将json反序列化为指定类型的对象
        /// </summary>
        /// <param name="json">json</param>
        /// <param name="targetType">目标类型</param>
        /// <returns>结果对象</returns>
        protected abstract object PrimitiveJsonDeserialize(string json, Type targetType);


    }

}
